<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../../lib/innerIframe-include-ext.js"></script>
<script type="text/javascript" src="../../lib/loader-theme.js"></script>
<script type="text/javascript" src="../../lib/ext/locale/ext-lang-zh_CN.js"></script>
<script type="text/javascript" src="../../lib/ext/plugins/DataTip.js"></script>
<title>院系管理</title>
</head>
<script type="text/javascript">
Ext.require(['*']);
Ext.onReady(function(){
    Ext.QuickTips.init();
    Ext.define('webuser', {
        extend: 'Ext.data.Model',
        fields: [
            'location_id','location_name','dis_limit','detail_location','latitude','longitude','dt_am','dt_pm'
        ],
        idProperty: 'location_id'
    });
    
    var store = Ext.create('Ext.data.Store', {
        pageSize: 10,
        model: 'webuser',
        remoteSort: true,
        proxy: {
            type: 'ajax',
            method: 'GET',
            url: '/location/list',
            reader: {
                type: 'json',
                root: 'data',
                totalProperty: 'count'
            },
            simpleSortMode: true
        },
        sorters: [
            {
                property: 'location_id'
            }
        ]
    });
    
    var grid = Ext.create('Ext.grid.Panel', {
        id:'my_grid',
        store: store,
        loadMask: true,
        region: 'center',
        selModel: Ext.create('Ext.selection.CheckboxModel'),
        columns: [
            {
                text: "实习地点代码",
                dataIndex: 'location_id',
                hidden: false,
                width: 150
            },{
                text: "实习地点名称",
                dataIndex: 'location_name',
                width: 150
            },{
                text: '最大签到距离(米)',
                dataIndex: 'dis_limit',
                width: 150
            },{
                text: '详细地址',
                dataIndex: 'detail_location',
                width: 450
            },{
                text: '签到时间(HHmm)',
                dataIndex: 'dt_am',
                width: 150
            },{
                text: '签退时间(HHmm)',
                dataIndex: 'dt_pm',
                width: 150
            },{
                text: '纬度',
                dataIndex: 'latitude',
                hidden: true
            },{
                text: '经度',
                dataIndex: 'longitude',
                hidden: true
            }
        ],
        bbar: Ext.create('Ext.PagingToolbar', {
            store: store,
            displayInfo: true,
            displayMsg: '当前显示  {0} - {1} 共  {2} 条',
            emptyMsg: "没有数据显示！",
            beforePageText: "页码",
            afterPageText: "共 {0} 页",
            firstText:"首页",
            lastText:"末页",
            nextText:"下一页",
            prevText:"上一页",
            refreshText:"刷新"
        }),
        tbar: [
            {
                pressed: false,
                icon : '../../img/add.png',
                text: "添  加",
                disable:false,
                handler: addHandler
            },{
                pressed: false,
                icon : '../../img/edit.png',
                text: "修  改",
                disable:false,
                handler: updHandler
            },{
                icon : '../../img/delete.png',
                text : '删  除',
                disable:false,
                handler : delHandler
            }]
    });
    store.loadPage(1);

    var viewport = Ext.create('Ext.Viewport', {
        id: 'border-example',
        layout: 'fit',
        items: grid
    });

    var mapWin;

    var addWin;
    var form;
    form = new Ext.FormPanel({
        id: 'myForm',
        //url:'/dept/add',
        method:'POST',
        frame:true,
        style:'border-width:0 0 0 0;',
        bodyBorder:false,
        bodyPadding: '0 40 0 0',
        layout: 'form',
        plugins: {
            ptype: 'datatip'
        },
        items: [
            {
                xtype:'hidden',
                name:'location_id',
                labelAlign: 'right',
                id:'location_id',
                width: 100
            },{
                xtype:'hidden',
                name:'latitude',
                labelAlign: 'right',
                id:'latitude',
                width: 100
            },{
                xtype:'hidden',
                name:'longitude',
                id:'longitude',
                labelAlign: 'right',
                width: 100
            },{
                xtype:'textfield',
                fieldLabel:'实习地点名称',
                name:'location_name',
                labelAlign: 'right',
                maxLength:60,
                allowBlank:false,
                tooltip:'请输入实习地点名称',
                id:'location_name',
                width: 100
            },{
                xtype:'textfield',
                fieldLabel:'最大签到距离(米)',
                name:'dis_limit',
                labelAlign: 'right',
                maxLength:35,
                allowBlank:false,
                tooltip:'最大签到距离(米)',
                id:'dis_limit',
                vtype: 'alphanum',
                width: 100
            },{
                xtype:'textfield',
                fieldLabel: '详细地址',
                name:'detail_location',
                labelAlign: 'right',
                maxLength:100,
                allowBlank:false,
                readOnly:true,
                width: 200
            },{
                xtype:'textfield',
                fieldLabel:'签到时间(HHmm)',
                name:'dt_am',
                labelAlign: 'right',
                maxLength:4,
                allowBlank:false,
                tooltip:'请输入签到时间',
                id:'dt_am',
                width: 100
            },{
                xtype:'textfield',
                fieldLabel:'签退时间(HHmm)',
                name:'dt_pm',
                labelAlign: 'right',
                maxLength:4,
                allowBlank:false,
                tooltip:'请输入签退时间',
                id:'dt_pm',
                width: 100
            }
        ],
        buttons:[{
            text:'编辑地址',
            icon : '../../img/edit.png',
            handler: function(){
                //Ext.Msg.alert('信息提示', "测试");

                var lat = Ext.getCmp('myForm').getForm().findField('latitude').getValue();
                var lng = Ext.getCmp('myForm').getForm().findField('longitude').getValue();
                var coordStr;

                if(lat && lng){
                    coordStr = "&coord=" + lat + "," + lng
                }else{
                    coordStr = "";
                }

                //console.log(coordStr);

                mapWin = Ext.create('Ext.window.Window', {
                    title:'编辑实习地址',
                    width: 500,
                    height: 500,
                    layout: 'fit',
                    html:'<iframe id="mapPage" width="100%" height="100%" frameborder=0 src="http://apis.map.qq.com/tools/locpicker?search=1&type=1&key=6OUBZ-I5LRW-33BRQ-RGHUN-KRILS-3UF2T&referer=测试页面' + coordStr + '"></iframe>'
                });

                mapWin.show();
            }
        },{ 
            text:'保  存',
            icon:'../../img/accept.png',
            handler:function(){
                if(!form.getForm().isValid()){
                     Ext.Msg.alert('信息提示','请检查输入项是否按要求填写！');
                     return;
                }
                form.form.submit({
                    waitMsg:'正在保存数据...',
                    success:function(form,action){
                        if(action.result.success){
                            Ext.Msg.alert('信息提示',JSON.parse(action.response.responseText).msg,function(){
                                store.reload();
                                Ext.getCmp('myForm').getForm().reset();
                                addWin.close();
                            });
                        } else {
                            Ext.Msg.alert('信息提示',JSON.parse(action.response.responseText).msg);
                        }
                    },
                    failure:function(form,action){
                        Ext.Msg.alert('信息提示', JSON.parse(action.response.responseText).msg);
                    }
                });
            }
        },{
            text:'关  闭',
            icon:'../../img/close.png',
            handler:function(){
                this.up('form').getForm().reset();
                addWin.close();
            }
        }]
    });
    
    function updHandler(){
        var records = grid.getSelectionModel().getSelection();
        if(records.length > 1){
            Ext.Msg.alert("信息提示","只能选择一条数据进行修改！");
            return;
        } 
        if(records.length == 0){
            Ext.Msg.alert("信息提示","请选择需要修改的数据！");
            return;
        }
        //updForm.form.reset();
        if(!addWin){
            addWin = Ext.create('Ext.window.Window', {
                title: '修改实习地址信息',
                width: 400,
                height: 400,
                constrainHeader: true,
                closeAction:'hide',
                layout: 'fit',
                items: form
            });
        }else{
            addWin.setTitle('修改实习地址信息');
        }
        form.form.loadRecord(records[0]);
        //form.getForm().findField('department_cd').setReadOnly(true);
        //Ext.getCmp('param_cd').setValue('');
        form.getForm().url='/location/upd';
        addWin.show();
    }
    
    function addHandler(){
        if(!addWin){
            addWin = Ext.create('Ext.window.Window', {
                title: '添加实习地址信息',
                width: 400,
                height: 400,
                //bodyPadding: '0 0 0',
                //'z-index': 9999999,
                constrainHeader: true,
                layout: 'fit',
                closeAction:'hide',
                items: form
            });
        }else{
            addWin.setTitle('添加实习地址信息');
        }
        //form.getForm().findField('location_id').setReadOnly(false);
        form.getForm().url='/location/add';
        addWin.show();
    }
    
    function delHandler(){
        var records = grid.getSelectionModel().getSelection();
        if(records.length != 1){
            Ext.Msg.alert("信息提示","请选择一条需要删除的数据！");
            return;
        }
        Ext.MessageBox.confirm('信息提示','确认删除吗？',function(val){
            if(val == 'yes'){
                Ext.Ajax.request({
                    url: '/location/del/'+records[0].get("location_id"),
                    method : 'POST',
                    success: function(response, opts) {
                        var data = eval("("+response.responseText+")");
                        Ext.Msg.alert('信息提示',data.msg);
                        store.reload();
                    },
                    failure: function(response, opts) {
                        var data = eval("("+response.responseText+")");
                        if(!data.success){
                            Ext.Msg.alert('信息提示',data.msg);
                        } else {
                            Ext.Msg.alert("信息提示","系统忙，请稍候再试");
                        }
                    }
                });
            }
        });
    }

    window.addEventListener('message', function(event) {
        // 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
        var loc = event.data;
        if (loc && loc.module == 'locationPicker') {//防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
            //console.log(loc.latlng.lat, loc.latlng.lng);
            Ext.getCmp('myForm').getForm().findField('latitude').setValue(loc.latlng.lat);
            Ext.getCmp('myForm').getForm().findField('longitude').setValue(loc.latlng.lng);
            Ext.getCmp('myForm').getForm().findField('detail_location').setValue(loc.poiaddress);
        }
        mapWin.close();
    }, false);
});
</script>
<body>
</body>
</html>
